package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;

import com.bumptech.glide.Glide;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.User;
import com.hengxinguotong.zhihuichengjian.treeview.RecyclerAdapter;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @CopyRight 恒信国通信息科技
 * @Author WuLei
 * @Date 2019/1/16
 */

public class UserSelectAdapter extends RecyclerAdapter<User, UserSelectAdapter.UserSelectViewHolder> {
    private OnSendMessageListener onSendMessageListener;

    public UserSelectAdapter(Context context, List<User> dataList) {
        super(context, dataList);
    }

    @Override
    public UserSelectViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_user_select, null);
        return new UserSelectViewHolder(view);
    }

    public void setOnSendMessageListener(OnSendMessageListener onSendMessageListener) {
        this.onSendMessageListener = onSendMessageListener;
    }

    @Override
    public void onBindViewHolder(UserSelectViewHolder holder, int position) {
        holder.bindData(position);
    }

    public class UserSelectViewHolder extends RecyclerAdapter.BaseViewHolder<User>{
        @BindView(R.id.bg_tv)
        HXTextView bgTv;
        @BindView(R.id.head_portrait_img)
        ImageView headPortraitImg;
        @BindView(R.id.name_tv)
        HXTextView nameTv;
        @BindView(R.id.send_message_switch)
        Switch sendMessageSwitch;

        public UserSelectViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void bindData(final int position) {
            final User user = dataList.get(position);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            itemView.setLayoutParams(params);
            if(user.isChecked()){
                bgTv.setEnabled(false);
            }else {
                bgTv.setEnabled(true);
            }
            Glide.with(context)
                    .load(user.getHeadPortrait())
                    .error(R.mipmap.icon_head_potrait)
                    .placeholder(R.mipmap.icon_head_potrait)
                    .into(headPortraitImg);
            nameTv.setText(user.getName());
            if(user.getStatus() == 0){
                sendMessageSwitch.setChecked(false);
            }else if(user.getStatus() == 1){
                sendMessageSwitch.setChecked(true);
            }
            sendMessageSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    onSendMessageListener.OnSendMessage(position, isChecked);
                }
            });
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickListener.onClick(user);
                }
            });
        }
    }

    public interface OnSendMessageListener{
        void OnSendMessage(int position, boolean isChecked);
    }

}
